#!/bin/bash

# 简化版设备指令下发API - cURL测试脚本
# 使用方法: bash docs/simple_curl_test.sh

# API配置
API_KEY="ak_3d97ebf664db97f3023b336f5fc0ad23"
SECRET_KEY="e61b4d35cf2a29e46e08e36dd075ea585614cd91470719c4411d2b946f677e12"
BASE_URL="http://localhost:8000"

# 生成签名的函数
generate_signature() {
    local method="$1"
    local path="$2"
    local body="$3"
    local timestamp=$(date +%s)
    
    # 构建待签名字符串
    local string_to_sign="${method}\n${path}\n${timestamp}\n${body}"
    
    # 使用PHP计算签名
    local signature=$(php -r "echo hash_hmac('sha256', '${string_to_sign}', '${SECRET_KEY}');")
    
    echo "${timestamp}|${signature}"
}

echo "=== 简化版设备指令下发API测试 ==="
echo "新格式: 只需要 imei + type"
echo "API Key: ${API_KEY}"
echo "Base URL: ${BASE_URL}"
echo ""

# 测试1: 设备重启
echo "=== 测试1: 设备重启 ==="
METHOD="POST"
PATH="/api/device/command"
BODY='{"imei":"863780074784151","type":"reboot"}'

TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "请求: $BODY"
echo "时间戳: $TIMESTAMP"
echo "签名: $SIGNATURE"
echo ""

curl -X POST "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -d "${BODY}" \
  -w "\n状态码: %{http_code}\n\n"

echo "----------------------------------------"

# 测试2: 热点配置
echo "=== 测试2: 热点配置 ==="
BODY='{"imei":"863780074784151","type":"hotspot_config"}'

TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "请求: $BODY"
curl -X POST "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -d "${BODY}" \
  -w "\n状态码: %{http_code}\n\n"

echo "----------------------------------------"

# 测试3: SIM卡切换到槽1
echo "=== 测试3: SIM卡切换到槽1 ==="
BODY='{"imei":"863780074784151","type":"sim_switch_slot1"}'

TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "请求: $BODY"
curl -X POST "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -d "${BODY}" \
  -w "\n状态码: %{http_code}\n\n"

echo "----------------------------------------"

# 测试4: 网络限速10MB
echo "=== 测试4: 网络限速10MB ==="
BODY='{"imei":"863780074784151","type":"speed_limit_10m"}'

TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "请求: $BODY"
curl -X POST "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -d "${BODY}" \
  -w "\n状态码: %{http_code}\n\n"

echo "----------------------------------------"

# 测试5: 立即上报状态
echo "=== 测试5: 立即上报状态 ==="
BODY='{"imei":"863780074784151","type":"report_status"}'

TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "请求: $BODY"
curl -X POST "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -d "${BODY}" \
  -w "\n状态码: %{http_code}\n\n"

echo "----------------------------------------"

# 测试6: 查看支持的指令类型
echo "=== 测试6: 查看支持的指令类型 ==="
METHOD="GET"
PATH="/api/device/commands"
BODY=""

TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "GET请求: ${BASE_URL}${PATH}"
curl -X GET "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -w "\n状态码: %{http_code}\n\n"

echo "----------------------------------------"

# 测试7: 查看API示例
echo "=== 测试7: 查看API示例 ==="
PATH="/api/device/command/examples"

TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "GET请求: ${BASE_URL}${PATH}"
curl -X GET "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -w "\n状态码: %{http_code}\n\n"

echo "=== 测试完成 ==="
echo ""
echo "支持的指令类型："
echo "- reboot: 设备重启"
echo "- power_off: 设备关机"
echo "- factory_reset: 恢复出厂设置"
echo "- hotspot_enable: 开启热点"
echo "- hotspot_disable: 关闭热点"
echo "- hotspot_config: 配置热点"
echo "- sim_switch_slot1: 切换到SIM卡槽1"
echo "- sim_switch_slot2: 切换到SIM卡槽2"
echo "- speed_limit_10m: 限速10MB"
echo "- speed_limit_50m: 限速50MB"
echo "- speed_limit_100m: 限速100MB"
echo "- speed_unlimited: 取消限速"
echo "- report_status: 立即上报状态"
echo "- device_enable: 启用设备"
echo "- device_disable: 禁用设备"
